package com.wht.bill.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
import com.wht.bill.R;
import com.wht.bill.bean.BillBean;
import com.wht.bill.bean.BillDetailBean;
import com.wht.bill.bean.OutcomeClassifyBean;
import com.wht.bill.stickyheader.StickyHeaderGridAdapter;
import com.wht.bill.utils.CacheUtils;
import com.wht.bill.utils.DataUtils;
import com.wht.bill.utils.DialogUtil;
import com.wht.bill.view.SwipeMenuView;

import java.util.List;

/**
 * 悬浮头部项
 * 可侧滑删除
 */

public class TallyDetailAdapter extends StickyHeaderGridAdapter {

    private Context mContext;
    public onRefreshListener mOnRefreshListener;
    public interface onRefreshListener {
        void onRefresh();
    }
    private List<BillDetailBean.DaylistBean> mDatas;

    public void setmDatas(List<BillDetailBean.DaylistBean> mDatas) {
        this.mDatas = mDatas;
    }

    public TallyDetailAdapter(Context context, List<BillDetailBean.DaylistBean> datas,onRefreshListener pOnRefreshListener) {
        this.mContext = context;
        this.mDatas = datas;
        mOnRefreshListener = pOnRefreshListener;
    }


    @Override
    public int getSectionCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public int getSectionItemCount(int section) {
        return (mDatas == null || mDatas.get(section).list == null) ? 0 : mDatas.get(section).list.size();
    }

    @Override
    public StickyHeaderGridAdapter.HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_header, parent, false);
        return new MyHeaderViewHolder(view);
    }

    @Override
    public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_item, parent, false);
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int section) {
        final MyHeaderViewHolder holder = (MyHeaderViewHolder) viewHolder;
        holder.header_date.setText(mDatas.get(section).time + "日");
        holder.header_money.setText("支出 " + (!TextUtils.isEmpty(mDatas.get(section).outmoney) ? mDatas.get(section).outmoney : "0.00") + "  收入 " + (!TextUtils.isEmpty(mDatas.get(section).inmoney) ? mDatas.get(section).inmoney : "0.00"));
    }

    @Override
    public void onBindItemViewHolder(ItemViewHolder viewHolder, final int section, final int position) {
        final MyItemViewHolder holder = (MyItemViewHolder) viewHolder;
        String label = mDatas.get(section).list.get(position).classification;
        holder.item_title.setText(label);
        //备注
        if (!TextUtils.isEmpty(mDatas.get(section).list.get(position).desc)) {
            holder.item_desc.setText(mDatas.get(section).list.get(position).desc);
            holder.item_desc.setVisibility(View.VISIBLE);
        } else {
            holder.item_desc.setText("");
            holder.item_desc.setVisibility(View.GONE);
        }
        if (TextUtils.equals("1", mDatas.get(section).list.get(position).billType)) {
            //支出
            holder.item_money.setText(String.format("-%s", mDatas.get(section).list.get(position).money));
            holder.item_money.setTextColor(Color.parseColor("#05B092"));
            //设置icon
            for (OutcomeClassifyBean outcomeClassifyDatum : DataUtils.getOutcomeClassifyData()) {
                if (TextUtils.equals(label, outcomeClassifyDatum.name)) {
                    Glide.with(mContext).load(outcomeClassifyDatum.img).into(holder.item_img);
                    break;
                }
            }
        } else {
            //收入
            holder.item_money.setText(String.format("+%s", mDatas.get(section).list.get(position).money));
            holder.item_money.setTextColor(Color.parseColor("#FF4B4B"));
            for (OutcomeClassifyBean incomeClassifyDatum : DataUtils.getIncomeClassifyData()) {
                if (TextUtils.equals(label, incomeClassifyDatum.name)) {
                    Glide.with(mContext).load(incomeClassifyDatum.img).into(holder.item_img);
                    break;
                }
            }
        }

        holder.mSwipeMenuView.setSwipeEnable(false);

        //holder.item_delete.setOnClickListener(new View.OnClickListener() {
        //    @Override
        //    public void onClick(View v) {
        //        final int section = getAdapterPositionSection(holder.getAdapterPosition());
        //        final int offset = getItemSectionOffset(section, holder.getAdapterPosition());
        //
        //        mDatas.get(section).list.remove(offset);
        //        notifySectionItemRemoved(section, offset);
        //        Toast.makeText(holder.item_delete.getContext(), "删除--" + label, Toast.LENGTH_SHORT).show();
        //    }
        //});
        holder.item_layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View pView) {
                DialogUtil.showDeleteDialog(mContext, new DialogUtil.onConfirmClickListener() {
                    @Override
                    public void onConfirmClick() {
                        final int section = getAdapterPositionSection(holder.getAdapterPosition());
                        final int offset = getItemSectionOffset(section, holder.getAdapterPosition());
                        if (mDatas.get(section).list != null && !mDatas.get(section).list.isEmpty()) {
                            //获取要删除的实体
                            BillBean bean = mDatas.get(section).list.get(offset);
                            //获取账单列表
                            List<BillBean> billList = CacheUtils.getBillList();
                            //循环账单列表
                            for (BillBean billBean : billList) {
                                //在账单中找到要删除的账单数据
                                if (TextUtils.equals(bean.account, billBean.account)
                                        && TextUtils.equals(bean.money, billBean.money)
                                        && TextUtils.equals(bean.billType, billBean.billType)
                                        && TextUtils.equals(bean.time, billBean.time)) {
                                    //删除数据
                                    billList.remove(billBean);
                                    break;
                                }
                            }
                            //更新账单列表数据
                            MMKV kv = MMKV.mmkvWithID(CacheUtils.APP_CACHE_KEY);
                            kv.encode("bill", new Gson().toJson(billList));
                            //通过接口回调刷新数据列表
                            if (mOnRefreshListener != null) {
                                mOnRefreshListener.onRefresh();
                            }
                        }
                    }
                });
                return false;
            }
        });
    }

    public static class MyHeaderViewHolder extends HeaderViewHolder {
        TextView header_date;
        TextView header_money;

        MyHeaderViewHolder(View itemView) {
            super(itemView);
            header_date = (TextView) itemView.findViewById(R.id.header_date);
            header_money = (TextView) itemView.findViewById(R.id.header_money);
        }
    }

    public static class MyItemViewHolder extends ItemViewHolder {
        TextView item_title;
        TextView item_money;
        TextView item_desc;
        Button item_delete;
        ImageView item_img;
        RelativeLayout item_layout;
        SwipeMenuView mSwipeMenuView;

        MyItemViewHolder(View itemView) {
            super(itemView);
            item_title = (TextView) itemView.findViewById(R.id.item_title);
            item_money = (TextView) itemView.findViewById(R.id.item_money);
            item_delete = (Button) itemView.findViewById(R.id.item_delete);
            item_img = (ImageView) itemView.findViewById(R.id.item_img);
            item_layout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
            mSwipeMenuView = (SwipeMenuView) itemView.findViewById(R.id.swipe_menu);
            item_desc = (TextView) itemView.findViewById(R.id.item_desc);
        }
    }
}
